<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h1>添加员工信息</h1>

<form id="addform" name="addform" >
  员工姓名<input name="empName" type="text"><br>
  员工性别<input name="gender" type="radio" value="1" checked>男<input name="gender" type="radio" value="0">女<br>
  员工生日<input name="birthday" type="date"><br>
  入职日期<input name="hireDate" type="date"><br>
  工资<input name="salary" type="text"><br>
  住址<input name="address" type="text"><br>
  部门信息<select name="empDeptId" >


</select>
  <input id="button" type="button" onclick="addemp()" value="提交">
</form>
</body>
<script>
  $(document).ready(function () {
    loadDept();

  })

  function loadDept() {
    $.ajax({
      type: "get",
      dataType: "json",
      url: "/admin/emp/getdepts",
      success:function (result) {
        $("select").html("");
        var alldept = result.data;
        $("select").append(`<option value="${alldept[0].deptId}">${alldept[0].deptName}</option>`)
        for ( i = 1; i < alldept.length; i++) {
          var dept = alldept[i];
          $("select").append(`
          <option value="${dept.deptId}">${dept.deptName}</option>
          `)
        }
      }
    });
  }


  function addemp() {
    $.ajax({
      type: "get",
      dataType: "json",
      url: "/admin/emp/doaddjson",
      data: $("#addform").serialize(),
      success: function (result) {
        if (result.data > 0 ){
          window.location.href="/pages/emp_list.html";
        }else {
          alert(result.errorMsg)
        }
      }
    })
  }

  
  
</script>


</html>